using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using PluginHandler.UI;

namespace OraclePlugin.UI
{
    internal partial class ConnectionDialog : UserControl, IConnectionDialog
    {
        private DbConnectionStringBuilder _connectionStringBuilder;

        public ConnectionDialog()
        {
            InitializeComponent();
        }

        #region IConnectionDialog Member

        public DbConnectionStringBuilder ConnectionStringDetails
        {
            get 
            {
                if (_connectionStringBuilder == null)
                    _connectionStringBuilder = new DbConnectionStringBuilder();

                _connectionStringBuilder["Data Source"] = txtServer.Text;

                if (cmbAuthentication.SelectedIndex == 0)
                {
                    _connectionStringBuilder["User Id"] = txtUsername.Text;
                    _connectionStringBuilder["Password"] = txtPassword.Text;
                }
                else
                {
                    _connectionStringBuilder["User Id"] = "/";
                    _connectionStringBuilder.Remove("Password");
                }

                if (rdSysDba.Checked)
                {
                    _connectionStringBuilder["DBA Privilege"] = "SYSDBA";
                }
                else if (rdSysOper.Checked)
                {
                    _connectionStringBuilder["DBA Privilege"] = "SYSOPER";
                }
                else
                {
                    _connectionStringBuilder.Remove("DBA Privilege");
                }

                return _connectionStringBuilder;
            }
            set 
            {
                _connectionStringBuilder = value;

                txtServer.Text = _connectionStringBuilder["Data Source"].ToString();

                if ((string)_connectionStringBuilder["User Id"] == "/")
                {
                    cmbAuthentication.SelectedIndex = 1;
                    txtUsername.Text = "";
                    txtPassword.Text = "";
                }
                else
                {
                    cmbAuthentication.SelectedIndex = 0;
                    txtUsername.Text = _connectionStringBuilder["User Id"].ToString();
                    txtPassword.Text = TryGetValue(_connectionStringBuilder, "Password");
                }

                if(_connectionStringBuilder.ContainsKey("DBA Privilege"))
                {
                    if((string)_connectionStringBuilder["DBA Privilege"] == "SYSDBA")
                    {
                        rdSysDba.Checked = true;
                    }
                    else if ((string)_connectionStringBuilder["DBA Privilege"] == "SYSOPER")
                    {
                        rdSysOper.Checked = true;
                    }
                }
                else
                {
                    rdNone.Checked = true;
                }
            }
        }

        public bool FilledComplete
        {
            get
            {
                bool filledComplete = false;

                filledComplete =
                    txtServer.Text.Trim().Length > 0;

                if (filledComplete && cmbAuthentication.SelectedIndex == 0)
                {
                    filledComplete = txtUsername.Text.Trim().Length > 0;
                }

                return filledComplete;
            }
        }

        public bool AskForPassword
        {
            get { return chkAskForPassword.Checked; }
            set { chkAskForPassword.Checked = value; }
        }
        #endregion

        private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 1)
            {
                pnlSQLAuthentication.Enabled = false;
            }
            else
            {
                pnlSQLAuthentication.Enabled = true;
            }
        }

        private void ConnectionDialog_Load(object sender, EventArgs e)
        {
            cmbAuthentication.SelectedIndex = 0;
        }

        private string TryGetValue(DbConnectionStringBuilder dbConnectionStringBuilder, string keyword)
        {
            if (dbConnectionStringBuilder.ContainsKey(keyword))
            {
                return dbConnectionStringBuilder[keyword].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
